﻿//using System;
//using UnityEngine;

//#if !UNITY_EDITOR
//#if UNITY_ANDROID || UNITY_IOS
//using GoogleMobileAds.Api;
//#endif
//#endif

//public class AdmodController : MonoBehaviour {

//    public static AdmodController Instance;

//#if !UNITY_EDITOR
//#if UNITY_ANDROID || UNITY_IOS
//    private RewardedAd m_RewardedAd;
//#endif
//#endif

//    void Awake() {
//        if (Instance != null) {
//            Destroy(gameObject);
//        } else {
//            Instance = this;
//            DontDestroyOnLoad(gameObject);
//        }
//    }
//    void Start() {
//#if !UNITY_EDITOR
//#if UNITY_ANDROID || UNITY_IOS
//        MobileAds.Initialize(initStatus => {
//            CreateAndLoadRewardedAd();
//        });
//#endif
//#endif
//    }

//    private Coroutine m_Coroutine;

//    private void CreateAndLoadRewardedAd() {
//#if !UNITY_EDITOR
//#if UNITY_ANDROID
//        string adUnitId = "ca-app-pub-9546414920294035/8118286212";
//#elif UNITY_IOS
//        string adUnitId = "ca-app-pub-3321735106491906/7240256303";
//#endif
//#endif

//#if !UNITY_EDITOR
//#if UNITY_ANDROID || UNITY_IOS
//        var adRequest = new AdRequest();
//        RewardedAd.Load(adUnitId, adRequest, (RewardedAd ad, LoadAdError error) => {
//            if (m_Coroutine != null) {
//                StopCoroutine(m_Coroutine);
//            }
//            m_Coroutine = StartCoroutine(nameof(Wait));
//            if (error != null) {
//                UnityEngine.Debug.Log(error.GetMessage());
//                StopCoroutine(m_Coroutine);
//                m_Coroutine = StartCoroutine(nameof(Retry));
//                return;
//            }
//            UnityEngine.Debug.Log("RewardedAd Loaded");
//            m_RewardedAd = ad;
//            ListenToAdEvents(m_RewardedAd);
//        });
//#endif
//#endif
//    }

//#if !UNITY_EDITOR
//#if UNITY_ANDROID || UNITY_IOS

//    IEnumerator Wait() {
//        yield return new WaitForSeconds(3600);
//        m_Coroutine = null;
//        CreateAndLoadRewardedAd();
//    }

//    IEnumerator Retry() {
//        yield return new WaitForSeconds(1);
//        CreateAndLoadRewardedAd();
//    }


//    void ListenToAdEvents(RewardedAd rewardedAd) {
//        rewardedAd.OnAdPaid += (AdValue adValue) => {
//            m_IsRewardOK = true;
//        };
//        rewardedAd.OnAdImpressionRecorded += () => {
//        };
//        rewardedAd.OnAdClicked += () => {
//        };
//        rewardedAd.OnAdFullScreenContentOpened += () => {
//        };
//        rewardedAd.OnAdFullScreenContentClosed += () => {
//            HandleRewardedAdClosed();
//            CreateAndLoadRewardedAd();
//            DestroyAd(rewardedAd);
//        };
//        rewardedAd.OnAdFullScreenContentFailed += (AdError error) => {
//        };
//    }


//    void DestroyAd(RewardedAd rewardedAd) {
//        if (rewardedAd != null) {
//            rewardedAd.Destroy();
//        }
//    }

//    private void HandleRewardedAdClosed() {
//        if (m_IsRewardOK) {
//            m_OnComplete?.Invoke();
//        } else {
//            m_OnFail?.Invoke();
//        }
//        m_IsRewardOK = false;
//        m_OnComplete = null;
//        m_OnFail = null;
//    }

//#endif
//#endif

//    private bool m_IsRewardOK = false;
//    private Action m_OnComplete;
//    private Action m_OnFail;

//    public void ShowWitchCallback(Action onComplete, Action onFail) {
//#if !UNITY_EDITOR
//#if UNITY_ANDROID || UNITY_IOS
//        GameManager.Instance.isShowAds = true;
//#endif
//#endif
//        m_OnComplete = onComplete;
//        m_OnFail = onFail;
//        m_IsRewardOK = false;
//        UserChoseToWatchAd();
//    }

//    private void UserChoseToWatchAd() {
//#if !UNITY_EDITOR
//#if UNITY_ANDROID || UNITY_IOS
//        if (m_RewardedAd != null && m_RewardedAd.CanShowAd()) {
//            m_RewardedAd.Show((Reward reward) => {
//                // 广告已展示，用户获得了奖励。
//                m_IsRewardOK = true;
//            });
//        } else {
//            GameUIManager.Instance.popupNoads.SetActive(true);
//            Invoke(nameof(AutoHidenPopup), 3f);
//        }
//#else
//        GameUIManager.Instance.popupNoads.SetActive(true);
//        Invoke(nameof(AutoHidenPopup), 3f);
//#endif
//#else
//        GameUIManager.Instance.popupNoads.SetActive(true);
//        Invoke(nameof(AutoHidenPopup), 3f);
//#endif
//    }

//    private void AutoHidenPopup() {
//        GameUIManager.Instance.popupNoads.SetActive(false);
//    }
//}
